
## load packages 
# require(hdm)
# require(pforeach)
require(BridgeChange)
require(glmnet)
require(monomvn)
require(doParallel)
require(doMC)
require(doRNG)
require(mvnfast)
coef_bridge <- function(out){
    coef <- apply(out1, 2, mean)
    coef[grepl("beta", names(coef))]
}

## --------------------------------------- ##
## simulation setup: 
##  delta = n / p
##  rho = k / n
## --------------------------------------- ##

## --------------------------------------- ##
## cluster setup 
## --------------------------------------- ##
## i <- as.numeric(Sys.getenv("SLURM_ARRAY_TASK_ID")) # this should be 1--50
for(i in 1:length.out){

  cl <- makeCluster(detectCores()/2)
  registerDoParallel(cl)
  
    ## draw error term and n 
    set.seed(38193)
    n    <- floor(p * delta[i])
    eta  <- rnorm(n)
    etaz <- sigma * eta 

    ## doRNGseed(1234)
    sim_out <- foreach(j = 1:length(rho), .combine = "rbind",
                       .packages= c("BridgeChange", "glmnet", 'mvnfast')) %dorng% ({
    
    cat("Now at i = ", i, " j = ", j ,"\n")
    k <- ceiling(n * rho[j])

    l2_error <- save_bridge <- save_ridge <- save_en  <- rep(NA, n_iter)
    for (iter in 1:n_iter) {
      rm(y); rm(X);
        ## gen data ***************************************************************
        # X <- matrix(rnorm(n * p), nrow = n, ncol = p)
        X <<- as.matrix(faux::rnorm_multi(n = n,  mu = rep(0, p), r = 0.7))
        ## rmvn(n, rep(0, p), Sigma)
        beta <- c(rnorm(k, 0, 3), rep(0, p - k))
        y <<- X %*% beta + etaz

        ## fit lasso ***************************************************************
        # out <- rlasso(y = y, x = X, intercept = FALSE)
        # l2_error[iter] <- sqrt(sum((coef(out) - beta)^2)) / sqrt(sum(beta^2))
        out <- cv.glmnet(y = y, x= X, alpha = 1, nfolds = 3)
        l2_error[iter] <- sqrt(sum((coef(out, s = "lambda.min")[-1,] - beta)^2)) / sqrt(sum(beta^2))

        ## fit SparseChange ********************************************************
        out1 <- BridgeChangeReg(y~X, mcmc= 100, burn = 100, thin=1, verbose=0, n.break = 0)
        beta.bridge <- coef_bridge(out1)
        save_bridge[iter]  <- sqrt(sum((beta - beta.bridge)^2)) / sqrt(sum(beta^2))

        ## fit ridge ********************************************************
        rg_out <- cv.glmnet(y = y, x= X, alpha = 0, nfolds = 3)
        save_ridge[iter] <- sqrt(sum((coef(rg_out, s = "lambda.min")[-1,] - beta)^2)) / sqrt(sum(beta^2))

        ## fit elastic net **************************************************
        en_out <- cv.glmnet(y = y, x= X, alpha = 0.5, nfolds = 3)
        save_en[iter] <- sqrt(sum((coef(en_out, s = "lambda.min")[-1,] - beta)^2)) / sqrt(sum(beta^2))

    }

    mse.lasso   <- median(l2_error, na.rm = TRUE)
    mse.bridge  <- median(save_bridge, na.rm = TRUE)
    mse.ridge   <- median(save_ridge, na.rm = TRUE)
    mse.elastic <- median(save_en, na.rm = TRUE)


    out <- c(mse.lasso, mse.bridge, mse.ridge, mse.elastic)
    out
})

    cat("\nCurrent simulation is ", i, " iteration.\n")

    saveRDS(sim_out, file = paste("./nochange/corr07/res_coef/sim_coef_mse_corr07_", i, ".rds", sep = ''))
    
    
    stopCluster(cl)
    stopImplicitCluster()
}
